<?php
/**
 * StyleController is an instance of Controller (application.components). 
 *
 * Controller is an instance of CController (system.web).
 * CController manages a set of actions which deal with the corresponding user requests. 
 * Through the actions, CController coordinates the data flow between models and views. 
 *
 * Eduow :  International system for education (http://eduow.com)
 * Copyright 2010, Nikolay Stoitsev 
 *
 * Licensed under GNU General Public License version 2
 * Redistributions of files must retain the above copyright notice.
 *
 * @filesource
 * @copyright     Copyright 2010
 * @link          http://eduow.com Eduow Project
 * @package       eduow
 * @subpackage    application.controllers
 * @inheritance   class StyleController » Controller » CController » CBaseController » CComponent
 * @implements    
 * @since         Eduow v 0.0.1
 * @license       http://www.gnu.org/licenses/gpl-2.0.html GNU General Public License version 2
 */


class StyleController extends Controller
{
	/**
	 * @var string the default layout for the views. 
	 */
	public $layout='main';

	/**
	 * @var CActiveRecord the currently loaded data model instance.
	 */
	private $_model;

	/**
	 * @return array action filters
	 */
	public function filters()
	{
		return array(
			'accessControl', // perform access control for CRUD operations
		);
	}

	/**
	 * Specifies the access control rules.
	 * This method is used by the 'accessControl' filter.
	 * @return array access control rules
	 */
	public function accessUpdate()
	{
		return array(
			'update'=>array(Group::USER, 'min'),
		);
	}

	/**
	 * Render style editor
	 * Manage style editor functions
	 */
	public function actionIndex()
	{
		$model=$this->loadModel();

		if(isset($_POST['Style']))
		{
			$model->attributes=$_POST['Style'];
			if($model->save())
				Yii::app()->user->setFlash('model','Your style have been updated succesfully. Thank you!');
		}

		$this->render('update',array(
			'model'=>$model,
		));
	}

	/**
	 * Returns the data model based on the primary key given in the GET variable.
	 * If the data model is not found, an HTTP exception will be raised.
	 */
	public function loadModel()
	{
		if($this->_model===null)
		{
			$this->_model=Style::model()->find('user=:id',array(':id'=>Yii::app()->user->id));
			if($this->_model===null)
				throw new CHttpException(404,'Error with user.');
		}
		return $this->_model;
	}
}
